//! 字符集定义
//! 
//! 定义CharacterSet结构体和相关功能

use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use crate::config::Style;

/// 字符集，包含同一概念的不同样式表示
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CharacterSet {
    /// 简单字符样式
    pub simple: Option<String>,
    /// 图标样式
    pub icon: Option<String>,
    /// 粗体样式
    pub bold: Option<String>,
    /// 彩色样式
    pub color: Option<String>,
    /// 自定义样式映射
    pub custom: HashMap<String, String>,
}

impl CharacterSet {
    /// 创建新的字符集
    pub fn new() -> Self {
        Self {
            simple: None,
            icon: None,
            bold: None,
            color: None,
            custom: HashMap::new(),
        }
    }

    /// 设置简单字符
    pub fn simple(mut self, char: &str) -> Self {
        self.simple = Some(char.to_string());
        self
    }

    /// 设置图标字符
    pub fn icon(mut self, char: &str) -> Self {
        self.icon = Some(char.to_string());
        self
    }

    /// 设置粗体字符
    pub fn bold(mut self, char: &str) -> Self {
        self.bold = Some(char.to_string());
        self
    }

    /// 设置彩色字符
    pub fn color(mut self, char: &str) -> Self {
        self.color = Some(char.to_string());
        self
    }

    /// 添加自定义样式字符
    pub fn custom(mut self, style_name: &str, char: &str) -> Self {
        self.custom.insert(style_name.to_string(), char.to_string());
        self
    }

    /// 根据样式获取字符
    pub fn get(&self, style: &Style) -> Option<&str> {
        match style {
            Style::Simple => self.simple.as_deref(),
            Style::Icon => self.icon.as_deref(),
            Style::Bold => self.bold.as_deref(),
            Style::Color => self.color.as_deref(),
            Style::Custom(name) => self.custom.get(name).map(|s| s.as_str()),
        }
    }

    /// 获取所有可用的样式
    pub fn available_styles(&self) -> Vec<Style> {
        let mut styles = Vec::new();
        
        if self.simple.is_some() {
            styles.push(Style::Simple);
        }
        if self.icon.is_some() {
            styles.push(Style::Icon);
        }
        if self.bold.is_some() {
            styles.push(Style::Bold);
        }
        if self.color.is_some() {
            styles.push(Style::Color);
        }
        
        for custom_name in self.custom.keys() {
            styles.push(Style::Custom(custom_name.clone()));
        }
        
        styles
    }
}

impl Default for CharacterSet {
    fn default() -> Self {
        Self::new()
    }
}
